package gui_xmlparser;

/**
 *
 * Rivera, Valencia, De Grano, Tan, Macatangay
 */

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class SAXhandler extends DefaultHandler{
	private ArrayList<Constituent> ConstList;
	private ArrayList<Constituent> SubList;
	private ArrayList<Constituent> InnerSubList;
	private ArrayList<Constituent> tempSubList = new ArrayList<Constituent>();
	private ArrayList<Feature> FeatureList;
		
	private int CurrentSubIndex = 0;
	private int CurrentDepth = -1;
		
	private Feature tempFeature;
		
	private boolean subFlag = false;
	private boolean label = false;
	private boolean concept = false;
	private boolean name = false;
	private boolean value = false;
		
		
	public void startElement(String uri, String localName,String qName,Attributes attributes) throws SAXException 
	{
		if(qName.equalsIgnoreCase("constituent"))
		{
			if(ConstList == null){
				ConstList = new ArrayList<Constituent>();
				SubList = new ArrayList<Constituent>();
			}
			Constituent tempConst = new Constituent();
			ConstList.add(tempConst);
		}
			
		if(qName.equalsIgnoreCase("subconstituent"))
		{
			if(subFlag == false)
			{
				InnerSubList = new ArrayList<Constituent>();
				Constituent tempConst = new Constituent();
				SubList.add(tempConst);
			}	
			else
			{
				Constituent tempConst = new Constituent();
				InnerSubList.add(tempConst);
			}
				
			CurrentDepth++;
			subFlag=true;
		}
			
		if(qName.equalsIgnoreCase("label"))
			label = true;
		
		if(qName.equalsIgnoreCase("concept"))
			concept = true;
			
		if(qName.equalsIgnoreCase("features"))
			FeatureList = new ArrayList<Feature>();
			
		if(qName.equalsIgnoreCase("name"))
		{
			name = true;
			tempFeature = new Feature();
		}
			
		if(qName.equalsIgnoreCase("value"))
			value = true;
						
	}
	public void characters(char ch[], int start, int length) throws SAXException 
	{
		if(label)
		{
			if(!subFlag)
			{
				int CurrentIndex = ConstList.size()-1;
				ConstList.get(CurrentIndex).setLabel(new String(ch,start,length));
			}
			else if(CurrentDepth==0)
			{
				int CurrentIndex = SubList.size()-1;
				SubList.get(CurrentIndex).setLabel(new String(ch,start,length));
			}
			else
			{
				int CurrentIndex = InnerSubList.size()-1;
				InnerSubList.get(CurrentIndex).setLabel(new String(ch,start,length));
			}
		}
		
		if(concept)
		{
			if(!subFlag)
			{
				int CurrentIndex = ConstList.size()-1;
				ConstList.get(CurrentIndex).setConcept(new String(ch,start,length));
			}
			else if(CurrentDepth==0)
			{
				int CurrentIndex = SubList.size()-1;
				SubList.get(CurrentIndex).setConcept(new String(ch,start,length));
			}
			else
			{
				int CurrentIndex = InnerSubList.size()-1;
				InnerSubList.get(CurrentIndex).setConcept(new String(ch,start,length));
			}
		}
			
		if(name)
			tempFeature.setName(new String(ch,start,length));
		if(value)
			tempFeature.setValue(new String(ch,start,length));
	}
	
	public void endElement(String uri, String localName,String qName) throws SAXException 
	{
		if(qName.equalsIgnoreCase("subconstituent"))
		{
			CurrentDepth--;
			
			if(CurrentDepth == -1)
			{
				subFlag = false;
				Subconstituent tempSubconstituent =  new Subconstituent();
				tempSubconstituent.setConstList(InnerSubList);
				int CurrentIndex = SubList.size();
				SubList.get(CurrentIndex-1).setSubconstituents(tempSubconstituent);
				CurrentSubIndex = 0;
			}
			else if(InnerSubList.size()-1<=CurrentSubIndex)
			{
				if(InnerSubList.size()-1==CurrentSubIndex)
				{
					Subconstituent tempSubconstituent =  new Subconstituent();
					tempSubconstituent.setConstList(tempSubList);
					
					InnerSubList.get(CurrentSubIndex).setSubconstituents(tempSubconstituent);
					tempSubList = new ArrayList<Constituent>();
					CurrentSubIndex++;
				}
			}
			else
			{
				Constituent tempConstituent = new Constituent();
				int CurrentIndex = InnerSubList.size();
				tempConstituent = InnerSubList.get(CurrentIndex-1);
				InnerSubList.remove(CurrentIndex-1);
				tempSubList.add(tempConstituent);
			}
		}
			
		if(qName.equalsIgnoreCase("label"))
			label = false;
		
		if(qName.equalsIgnoreCase("concept"))
			concept = false;
		
		if(qName.equalsIgnoreCase("features"))
		{
			Features features = new Features(FeatureList);
				
			if(!subFlag)
			{
				int CurrentIndex = ConstList.size()-1;
				ConstList.get(CurrentIndex).setFeatures(features);
			}
			else if(CurrentDepth==0)
			{
				int CurrentIndex = SubList.size()-1;
				SubList.get(CurrentIndex).setFeatures(features);
			}
			else
			{
				int CurrentIndex = InnerSubList.size()-1;
				InnerSubList.get(CurrentIndex).setFeatures(features);
			}
		}
			
		if(qName.equalsIgnoreCase("name"))
			name = false;
		if(qName.equalsIgnoreCase("value"))
		{
			FeatureList.add(tempFeature);
			value = false;
		}
		
		if(qName.equalsIgnoreCase("constituent"))
		{
			int CurrentIndex = ConstList.size()-1;
			Subconstituent tempSubconstituent = new Subconstituent();
			tempSubconstituent.setConstList(SubList);
			ConstList.get(CurrentIndex).setSubconstituents(tempSubconstituent);
		}
	}
	
	public ArrayList<Constituent> getConstList() {
		return ConstList;
	}
}
